// dlltool.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "dlltool.h"
//#include <detours21\detours.h>
#include <atlbase.h>
CComModule _Module;
#include <atlwin.h>
#pragma comment(lib,"atl")

//#include <atlcom.h>
//#include <ATLHOST.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CDlltoolApp

BEGIN_MESSAGE_MAP(CDlltoolApp, CWinApp)
	//{{AFX_MSG_MAP(CDlltoolApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlltoolApp construction

CDlltoolApp::CDlltoolApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDlltoolApp object

DWORD WINAPI ThreadProc(
  LPVOID lpParameter
)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CDlgMain dlg;
	dlg.DoModal();
	return 0;
}
LRESULT (WINAPI * MyRealDispatchMessageA)( CONST MSG *lpMsg)  = DispatchMessageA;
// Detour function that replaces the Sleep API.
LRESULT WINAPI MyDispatchMessageA( CONST MSG *lpMsg)
{
    // Save the before and after times around calling the Sleep API.
	if(lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F8)
	{
		DWORD THID;
		CreateThread(NULL,0,ThreadProc,NULL,0,&THID);
	}
	return MyRealDispatchMessageA(lpMsg);
}


LRESULT (WINAPI * MyRealDispatchMessageW)( CONST MSG *lpMsg)  = DispatchMessageW;
// Detour function that replaces the Sleep API.
LRESULT WINAPI MyDispatchMessageW( CONST MSG *lpMsg)
{
    // Save the before and after times around calling the Sleep API.
	if(lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F8)
	{
		DWORD THID;
		CreateThread(NULL,0,ThreadProc,NULL,0,&THID);

	}
	return MyRealDispatchMessageW(lpMsg);
}

BOOL (WINAPI * MyRealGetVersionExW)(LPOSVERSIONINFOW lpVersionInformation)  = GetVersionExW;
// Detour function that replaces the Sleep API.
BOOL WINAPI MyGetVersionExW(LPOSVERSIONINFOW lpVersionInformation)
{
	// Save the before and after times around calling the Sleep API.
	BOOL bRet;
	bRet = MyRealGetVersionExW(lpVersionInformation);
	lpVersionInformation->dwMinorVersion = 1;
	return bRet;
}


CDlltoolApp theApp;

 void ShowDlg(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	AtlAxWinInit();
	CDlgMain dlg;
	dlg.DoModal();
}
void EntryShowDlg(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	AtlAxWinInit();
	CDlgMain dlg;
	//ASSERT(FALSE);
	dlg.DoModal();
}
BOOL CDlltoolApp::InitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	//theApp->m_hPrevInstance
	//theApp->m_hInstance

	GetModuleFileName(theApp.m_hInstance,g_szRootPath,512);
	for (int i = strlen(g_szRootPath);i>0;i--)
	{
		if (g_szRootPath[i]== '\\' ||g_szRootPath[i]== '/')
		{
			g_szRootPath[i]= 0;
			break;
		}
	}
//nitLogPath(g_szRootPath,"lclog.log");
	return CWinApp::InitInstance();
}

int CDlltoolApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWinApp::ExitInstance();
}
